package com.lgd.warehousemanagement.Utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lgd.warehousemanagement.R;

import java.lang.ref.WeakReference;


/**
 *@Author lgd
 * Toast  统一管理类
 */
public class ToastUtils {
	private static long lastShowTime = 0l;
	private ToastUtils() {
		/* cannot be instantiated 无法 实例化*/
		throw new UnsupportedOperationException("cannot be instantiated");  //   抛出异常
	}
	public static boolean isShow = true;

	/**
	 * 短时间显示Toast
	 *
	 * @param context
	 * @param message
	 */
	public static void showShort(Context context, CharSequence message) {
		if (isShow){
//			Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
			//  纯文本提示框 （居中）
			Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
			toast.setGravity(Gravity.CENTER,0,0);
			LinearLayout linearLayout = (LinearLayout) toast.getView();
			TextView messageTextView = (TextView) linearLayout.getChildAt(0);
			messageTextView.setTextSize(25);
			messageTextView.setTextColor(Color.parseColor("#FFFFFF"));
			toast.show();
		}
	}

	/**
	 * 短时间显示Toast
	 *
	 * @param context
	 * @param message
	 */
	public static void showShort(Context context, int message) {
		if (isShow)
			Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}
	/**
	 * 长时间显示Toast
	 *
	 * @param context
	 * @param message
	 */
	//  警告
//	public static void warnShowLong(Context context, CharSequence message) {
//		if (isShow){
//			long curShowTime = System.currentTimeMillis();
//				// 自定义带图标 弹窗样式
//				View toastView = LayoutInflater.from(context).inflate(R.layout.toast_tips_layout, null);
//				LinearLayout linearLayout = (LinearLayout)toastView.findViewById(R.id.toast_linear);
//				TextView textView = (TextView)toastView.findViewById(R.id.tv_toast_tips);
//				int width = ScreenUtils.getWidth(context);
//				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width * 5/10, ViewGroup.LayoutParams.WRAP_CONTENT);
//				linearLayout.setLayoutParams(layoutParams);
//				textView.setText(message);
//				Toast toast = new Toast(context);
//				toast.setDuration(Toast.LENGTH_LONG);
//				toast.setGravity(Gravity.CENTER, 0, 0);
//				toast.setView(toastView);
//				toast.show();
//		}
//	}
			//  解决 重复显示问题
//  解决 重复显示问题  currentToast 会造成内存泄漏问题
	private static WeakReference<Toast> currentToastWeakRef = null;
	public static Toast currentToast = null;
	public static void warnShowLong(Context context, CharSequence message) {
		if (isShow) {
			if (currentToast != null) {
				currentToast.cancel();
			}
			if (currentToastWeakRef != null && currentToastWeakRef.get() != null) {
				currentToastWeakRef.get().cancel();
			}
			long curShowTime = System.currentTimeMillis();
			View toastView = LayoutInflater.from(context).inflate(R.layout.toast_tips_layout, null);
			LinearLayout linearLayout = (LinearLayout) toastView.findViewById(R.id.toast_linear);
			TextView textView = (TextView) toastView.findViewById(R.id.tv_toast_tips);
			int width = ScreenUtils.getWidth(context);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width * 5 / 10, ViewGroup.LayoutParams.WRAP_CONTENT);
			linearLayout.setLayoutParams(layoutParams);
			textView.setText(message);

			Toast toast = new Toast(context);
			toast.setDuration(Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.setView(toastView);

			currentToastWeakRef = new WeakReference<>(toast);

			toast.show();
			currentToast = toast;
		}
	}

	// 成功
	public static void successShowLong(Context context,CharSequence message){
		if (isShow){
			View toastView = LayoutInflater.from(context).inflate(R.layout.toast_success_layout, null);
			LinearLayout linearLayout = (LinearLayout)toastView.findViewById(R.id.toast_linear_success);
			TextView textView = (TextView)toastView.findViewById(R.id.tv_toast_success);
			int width = ScreenUtils.getWidth(context);
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width * 5/10, ViewGroup.LayoutParams.WRAP_CONTENT);
			linearLayout.setLayoutParams(layoutParams);
			textView.setText(message);

			Toast toast = new Toast(context);
			toast.setDuration(Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.setView(toastView);
			toast.show();
		}

	}

	/**
	 * 长时间显示Toast
	 *
	 * @param context
	 * @param message
	 */
	public static void showLong(Context context, int message) {
		if (isShow)
			Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}

	/**
	 * 自定义显示Toast时间
	 *
	 * @param context
	 * @param message
	 * @param duration
	 */
	public static void show(Context context, CharSequence message, int duration) {
		if (isShow)
			Toast.makeText(context, message, duration).show();
	}

	/**
	 * 自定义显示Toast时间
	 *
	 * @param context
	 * @param message
	 * @param duration
	 */
	public static void show(Context context, int message, int duration) {
		if (isShow)
			Toast.makeText(context, message, duration).show();
	}

	public static void showUI(final Activity activity, final String message, final int duration) {
		if (isShow)
			activity.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(activity, message, duration).show();
				}
			});
	}

}